// rtk
import { configureStore, combineReducers } from '@reduxjs/toolkit';
// 引入插件
import logger from 'redux-logger';
// slice 切片模块
import carSlice from './car/carSlice'
import { useSelector, TypedUseSelectorHook } from 'react-redux';
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
 
const persistConfig = {
  key: '2104a',
  storage,
}

const rootReducer = combineReducers({
  car: carSlice.reducer
})
const persistedReducer = persistReducer(persistConfig, rootReducer)

// 仓库
const store = configureStore({
  // 纯函数
  reducer: persistedReducer,
  // 中间件
  middleware(getDefaultMiddleware) {
    // 获取默认中间件
    return getDefaultMiddleware({
      // 检查关闭
      serializableCheck: false
      // 使用logger 
    }).concat(logger)
  },
})

// 持久化
export const persistor = persistStore(store)
// 仓库状态
export type RootState = ReturnType<typeof store.getState>
// 重写 useSelectoor  非常好用
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
export default store